package com.smedi.ismedi.inmail.core.application.service.impl;

import com.smedi.ismedi.inmail.core.application.command.LoginLogLoginCommand;
import com.smedi.ismedi.inmail.core.application.command.LoginLogLogoutCommand;
import com.smedi.ismedi.inmail.core.application.command.OperationLogCreateCommand;
import com.smedi.ismedi.inmail.core.application.convert.AppConverter;
import com.smedi.ismedi.inmail.core.application.service.LoginLogApplicationService;
import com.smedi.ismedi.inmail.core.application.service.OperationLogApplicationService;
import com.smedi.ismedi.inmail.core.domain.aggregatemodel.LoginLog;
import com.smedi.ismedi.inmail.core.domain.aggregatemodel.OperationLog;
import com.smedi.ismedi.inmail.core.domain.service.LoginLogDomainService;
import com.smedi.ismedi.inmail.core.domain.service.OperationLogDomainService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * 通知公告应用服务实现类
 *
 * @author jiangqinxuan
 * @date 2021/07/26
 */
@Service
@RequiredArgsConstructor
@Transactional
@Slf4j
public class OperationLogApplicationServiceImpl implements OperationLogApplicationService {

    private final LoginLogDomainService loginLogDomainService;
    private final OperationLogDomainService operationLogDomainService;
    private final AppConverter appConverter;


    @Override
    public Long create(OperationLogCreateCommand operationLogCreateCommand) {
        loginLogDomainService.update(LoginLog.builder()
                .id(operationLogCreateCommand.getLoginLogId())
                .logoutTime(LocalDateTime.now())
                .build());
        OperationLog operationLog = appConverter.operationLogCreateCommandToOperationLog(operationLogCreateCommand);
        return operationLogDomainService.create(operationLog);
    }
}
